function ListComplete(response)
{
    //console.log(response);
    //$("#scoreSubmit").attr('value', 'done');
    if(response.data)
    {
        var scores = response.data;
        //console.log(scores);
        //alert(scores.length + " scores returned out of " + numscores);
	    var str = "<font size=6>Online Ranking</font><div style='height:95px;overflow:auto;'>";
        str += "<table cellpadding=0 cellspacing=0>" +
            "<tr><td width='25%'><b>Rank</b></td><td width='25%'><b>Name</b></td><td width='25%'><b>Score</b></td></tr>";		
        for(var i=0; i<scores.length; i++)
        {
            var score = scores[i].score;
            if(scores[i].user.id == FB.getUserID())
                str += "<tr bgcolor='#00ff00'><td> " + i + "<td>" + scores[i].user.name + "</td><td> &nbsp;&nbsp;" + scores[i].score + "</td></tr>";
            else
                str += "<tr><td> " + i + "<td>" + scores[i].user.name + "</td><td> &nbsp;&nbsp;" + scores[i].score + "</td></tr>";
            // including custom data?  score.CustomPlaytomic.Data.Property
        }
        str += "</table></div>";
        str += "<button onClick='g_gui.restartGame();'>Restart Game</button>";
        $("#gamePause").html(str);
    }
    else
    {
        // score listing failed because of response.ErrorCode
    }
}

function checkPost(response) //see if the score was posted.
{
    if(response)
        alert("yes");
    else
        alert("no");        
}

function SubmitScore()
{
//    $("#scoreName").attr('disabled', 'disabled');
    $("#scoreSubmit").attr('disabled', 'disabled');
    //simple_score.access_token ='AAADH8bEMLbQBAHyZAZBTZAZAy6kruziDUJtDLl6B42pYyjkzm7OMijHl3lT5awiSppvEEMrOi9ORoQ47LOglvXB0NiWw0GGouEeNG8KdJwZDZD';
    FB.api("/"+FB.getUserID()+"/scores", 'get', {}, function(data){
//            console.log(data.data[0]);
//            alert(data.data[0].score);
            var simple_score = {};
            simple_score.score = g_game.getScore();
            simple_score.client_id ='219840871411124';
            simple_score.client_secret ='468e1b0aa076ae284cdfc4670d5f7168';
            if(simple_score.score > data.data[0].score)
            {
                //post the score to facebook
                FB.api("/"+FB.getUserID()+"/scores", 'post', simple_score, function(data){});
            }
            FB.api("/"+simple_score.client_id+"/scores", 'get', {}, ListComplete);
    });
}

function fblogin(response){
    if(response.session){
        if(response.status == 'connected'){
            SubmitScore();
        }
    }
    else{
        //                    alert('Could not login to FB');
    }
}

function UpdatePauseUI(res)
{
    var str;
    str = "<p><b><font size=6>Score: " + g_game.getScore() + "<br/>Stars: " + g_game.stars_collected + "</font></b><br/>";
    if(res.status=='connected'){
        SubmitScore();
    }
    else{
        str += "<button id='scoreSubmit' onClick='FB.login(fblogin, {scope:\"publish_actions\"})'>FB-Connect</button><br/>";
    }
    str += "<br/><button onClick='g_gui.restartGame();'>Restart Game</button></p>";
    g_gui.pauseUI.html(str);
}

function GameUI()
{
    this.pauseUI = null; //pause menu
    this.gameUI = null; //stats ui
    this.menuUI = null;

    this.restartGame = function()
    {
        this.pauseUI.fadeOut('fast',null);
        g_game.init(document.getElementById("gameCanvas"), this);
        g_game.resume();
    }

    this.showScore = function()
    {
        //this.updatePauseUI();
        FB.getLoginStatus(function(res){UpdatePauseUI(res);});
        this.pauseUI.fadeIn('fast', null);
    }

    this.init = function(c)
    {
        var w = c.width;
        var h = c.height;
        var left = c.offsetLeft;
        var top = c.offsetTop;
        this.pauseUI = $("#gamePause");
        this.pauseUI.css("left",left);
        this.pauseUI.css("top",h/2-80);
        this.pauseUI.css("width", w + "px");
        this.pauseUI.css("height", "160px");
        this.pauseUI.fadeOut('fast', null);

        this.menuUI = $("#gameMain");
        this.menuUI.css("left",left);
        this.menuUI.css("top",h/2-80);
        this.menuUI.css("width", w + "px");
        this.menuUI.css("height", "160px");
        this.menuUI.html("<p><font size=6> <b>TGIF+</b></font> <br/> <b>Thank God It's Growing</b> <br/><br/> click to Start</p>");
        this.menuUI.click(function(){
                g_gui.menuUI.fadeOut('fast', function(){
                    //                g_game.resume();
                    $("#gameHelp").fadeIn('fast', null);
                    })
                });
        $("#gameHelp").hide();
        $("#gameHelp").css("left", left);
        $("#gameHelp").css("top",top+h/4+h/8);
        $("#gameHelp").css("width", w + "px");
        $("#gameHelp").css("height", h + "px");
        $("#gameHelp").click(function(){
                $("#gameHelp").fadeOut('fast',function(){
                    g_game.resume();
                    });
                });
        //set the cursor styles.
        $("#gameCanvas").css('cursor', 'crosshair');
        $("#gameHelp").css('cursor', 'pointer');
        //$("#gamePause").css('cursor', 'pointer');
        $("#gameMain").css('cursor', 'pointer');
    }
}

